<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/iconfont.css">
    <link rel="stylesheet" href="css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/template-web.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章类别管理
        </div>
        <div class="container-fluid common_con">
            <table class="table table-striped table-bordered table-hover mp20 category_table">
                <thead>
                    <tr>
                        <th>名称</th>
                        <th>Slug</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- <tr>
                        <td>奇趣事</td>
                        <td>funny</td>
                        <td class="text-center">
                            <a href="javascript;" data-toggle="modal" class=" btn btn-info btn-xs">编辑</a>
                            <a href="javascript;" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr> -->
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="3" class="text-center">
                            <a href="#" class="btn btn-success" id="xinzengfenlei" data-toggle="modal"
                                data-target="#exampleModal">新增分类</a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <!-- 模态框 -->
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">新增分类</h4>
                </div>
                <div class="modal-body">
                    <form>
                        <div class="form-group">
                            <label for="recipient-name" class="control-label">名称:</label>
                            <input type="text" class="form-control" id="recipient-name" name="name">
                            <span id="error-msg" style="display: none; color: red;">分类名已存在</span>
                        </div>
                        <div class="form-group">
                            <label for="message-text" class="control-label">slug:</label>
                            <textarea class="form-control" id="message-text" name="slug"></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary">保存</button>
                </div>
            </div>
        </div>
    </div>
</body>
<script src="./libs/http.js"></script>
<script id="cateTemp" type="text/html">
    {{each data v}} 
    <tr>
        <td>{{v.name}}</td>
        <td>{{v.slug}}</td>
        <td class="text-center">
            <a href="javascript:;" data-toggle="modal" class=" btn btn-info btn-xs" data-id="{{v.id}}" data-target="#exampleModal">编辑</a>
            <a href="javascript:;" class="btn btn-danger btn-xs" data-id="{{v.id}}">删除</a>
        </td>
    </tr>
    {{/each}}
</script>
<script>
    $(function () {
        let cateData = null;
        // 获取文章分类
        function getCate() {
            $.ajax({
                type: 'get',
                url: BigNew.category_list,
                success: function (backData) {
                    console.log(backData);
                    if (backData.code === 200) {
                        $('table>tbody').html(template('cateTemp', backData));
                        cateData = backData;
                    }
                }
            })
        }
        getCate();

        // 模态框弹出显示事件
        $('#exampleModal').on('show.bs.modal', function (event) {
            // if (event.relatedTarget.textContent === '新增分类') {
            //     $('#exampleModalLabel').text('新增分类');
            // } else {
            //     $('#exampleModalLabel').text('修改分类');
            // }
            $('#exampleModalLabel').text(event.relatedTarget.textContent === '新增分类' ? '新增分类' : '修改分类')
        })

        // 取消
        $('.modal-footer .btn-default').on('click', function () {
            $('#exampleModal form')[0].reset();
        })
        $('#recipient-name').on('focus', function () {
            $('#error-msg').hide();
        })
        $('#recipient-name').on('blur', function () {
            if ($('#exampleModalLabel').text() === '新增分类') {
                cateData.data.forEach(element => {
                    if ($('#recipient-name').val() === element.name) {
                        $('#error-msg').show()
                    }
                });
            }
        })

        let $editBtn = null;
        $('tbody').on('click', '.text-center .btn-info', function () {
            $('#recipient-name').val($(this).parent().prev().prev().text());
            $('#message-text').val($(this).parent().prev().text());
            $editBtn = $(this);
        })

        // 保存
        $('.modal-footer .btn-primary').on('click', function () {
            const name = $('#recipient-name').val().trim();
            const slug = $('#message-text').val().trim();
            // 有错误消息不保存
            if ($('#error-msg').css('display') === 'block' || !name || !slug) return;

            // 新增分类
            if ($('#exampleModalLabel').text() === '新增分类') {
                $.ajax({
                    type: 'post',
                    url: BigNew.category_add,
                    data: {
                        name,
                        slug
                    },
                    success: function (backData) {
                        if (backData.code === 201) {
                            getCate();
                            $('#exampleModal form')[0].reset();
                            $('#exampleModal').modal('hide');
                        }
                    }
                })
            } else {
                // 修改分类
                console.log($editBtn.attr('data-id'));
                $.ajax({
                    type: 'post',
                    url: BigNew.category_edit,
                    data: {
                        id: $editBtn.attr('data-id'),
                        name,
                        slug
                    },
                    success: function (backData) {
                        console.log(backData);
                        if (backData.code === 200) {
                            $editBtn.parent().prev().text(slug).prev().text(name);

                        }
                        $editBtn = null;
                        $('#exampleModal form')[0].reset();
                        $('#exampleModal').modal('hide');
                    }
                })
            }
        })

        // 删除
        $('tbody').on('click', '.text-center .btn-danger', function () {
            $.ajax({
                type: 'post',
                url: BigNew.category_delete,
                data: { id: $(this).attr('data-id') },
                success: (backData) => {
                    console.log(backData);
                    if (backData.code === 204) {
                        $(this).parent().parent().remove();
                    }
                }
            })
        })
    })
</script>

</html>